let mongo = require('mongoskin');
let db = mongo.db("mongodb://localhost:27017/wuhu", {native_parser: true});
const collectionName = 'questions';
let question = function () {
    this._id = undefined;
    this.title = '';
    this.content = '';
    //ObjectID
    this.answer = [];
    this.owner = undefined;
}
let answer = {
    content :'',
    owner : undefined,
}
/**
 *
 * @param para
 * question对象，其中title content 和owner需要设置
 * owner为User对象的id的string
 * @returns {Promise}
 */
question.prototype.save = function(para) {
    return new Promise(function (resolve, reject) {
        //console.log(this);
        db.collection(collectionName).insertOne({
                'title': para.title,
                'content': para.content,
                'answer': para.answer,
                'owner': para.owner
            },
            function (err, result) {
                db.close();
                if (err) {
                    reject(err.message);
                } else {
                    resolve(result);
                }
            })
    })
}
//example
// let q = new question();
// q.title = '为什么女人心难懂'
// q.content = '测试'
// q.owner = '5b2e23086f77a30f1e673004'
// q.save(q);
/**
 * 删除一个问题，通过他的id
 * @param para
 * question 必须要填_id号
 * @returns {Promise}
 */
question.prototype.delete = function (para) {
    return new Promise(function (resolve,reject) {
        db.collection(collectionName).removeById(para._id,function (err,result) {
            db.close();
            if (err) {
                reject(err.message);
            } else {
                resolve(result);
            }
        })
    })
}
//example
// let q = new question();
// //q.owner = '5b2e23086f77a30f1e673004'
// q.findByOwnerId('5b2e23086f77a30f1e673004').then(function (data) {
//     console.log(data);
//     q.delete(data[0]).then(function (data) {
//         console.log(data);
//     })
// })
question.prototype.findMany = function (page) {
    return new Promise(function (resolve,reject) {
        db.collection(collectionName).find().skip((page-1)*20).limit(20).toArray(function (err,res) {
            db.close();
                if (err) {
                    reject(err.message);
                } else {
                    resolve(res);
                }
        });
    })
}
question.prototype.findById = function (para) {
    return new Promise(function (resolve,reject) {
        db.collection(collectionName).findById(para._id,function (err,res) {
            db.close();
            if (err) {
                reject(err.message);
            } else {
                resolve(res);
            }
        })
    })

}
/**
 * 获取一个客户的所有问题
 * @param ownid
 *
 * @returns {Promise}
 */
question.prototype.findByOwnerId = function (ownid) {
    return new Promise(function (resolve,reject) {
        db.collection(collectionName).find({owner:ownid}).toArray(function (err,res) {
            db.close();
            if (err) {
                reject(err.message);
            } else {
                resolve(res);
            }
        });
    })
}
//example
// let q = new question();
// //q.owner = '5b2e23086f77a30f1e673004'
// q.findByOwnerId('5b2e23086f77a30f1e673004').then(function (data) {
//     console.log(data);
// })

/**
 *
 * @param para
 * question对象
 * @param answer
 * anwser
 */
question.prototype.updateOneAnswer = function (para,answer) {

    //console.log(oldAnswer);

    return new Promise(function (resolve,reject) {
        para.findById(para).then(function (data) {
            console.log(data);
            let oldAnswer = data.answer;
            oldAnswer.push(answer);
            db.collection(collectionName).updateById(para._id,{$set:{answer:oldAnswer}},function (err,res) {
                db.close();
                if (err) {
                    reject(err.message);
                } else {
                    resolve(res);
                }
            })
        });

    })

}
module.exports = {
    question: question,
    answer: answer
}